﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Drawing.Design;
using OpenNETCF.IO.Ports;

namespace robotGUI
{
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.MenuStrip |
                                       ToolStripItemDesignerAvailability.ContextMenuStrip |
                                       ToolStripItemDesignerAvailability.StatusStrip)]
    public class PictureStripItem : FixedToolStripControlHost
    {
        private PictureBox pic;

        public PictureStripItem()
            : base(new PictureBox())
        {
            this.pic = this.Control as PictureBox;
        }

        // Add properties, events etc. you want to expose...
        [Browsable(true),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public new Image Image { get { return pic.Image; } set { pic.Image = value; } }
        public PictureBoxSizeMode SizeMode { get { return pic.SizeMode; } set { pic.SizeMode = value; } }
    }
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.MenuStrip |
                                       ToolStripItemDesignerAvailability.ContextMenuStrip |
                                       ToolStripItemDesignerAvailability.StatusStrip)]
    public class SerialStripItem : FixedToolStripControlHost
    {
        public serialControl serialControl;

        public SerialStripItem()
            : base(new serialControl())
        {
            this.serialControl = this.Control as serialControl;
        }

        // Add properties, events etc. you want to expose...
        public SerialPort Port { get { return serialControl.Port; } set { serialControl.Port = value; } }
        public bool IsOpen { get { return serialControl.IsOpen; } }
        public void Close() { serialControl.Close(); }
        public void ForceClose() { serialControl.ForceClose(); }
        public void Write(byte[] buffer, int count) { serialControl.Write(buffer, count); }
        public void sendValue(Command cmd, byte[] values) { serialControl.sendValue(cmd, values); }
        public void sendValue(Command cmd, float value) { serialControl.sendValue(cmd, value); }
        public void sendValue(Command cmd, string value) { serialControl.sendValue(cmd, value); }
        public void readParam(Command param) { serialControl.readParam(param); }
        public void readEe(int address) { serialControl.readEe(address); }
        public void writeEe(int address, byte data) { serialControl.writeEe(address, data); }
        public void HeartBeat() { serialControl.HeartBeat(); }

        protected override void OnSubscribeControlEvents(Control c)
        {
            base.OnSubscribeControlEvents(c);
            serialControl sc = (serialControl)c;
            sc.PortClosed += new serialControl.PortClosedHandler(sc_PortClosed);
            sc.PortError += new serialControl.PortErrorEventHandler(sc_PortError);
            sc.PortOpened += new serialControl.PortOpenedHandler(sc_PortOpened);
            sc.PortOpening += new serialControl.PortOpeningHandler(sc_PortOpening);
            sc.DataReceived += new serialControl.DataReceivedEventHandler(sc_DataReceived);
        }
        public event serialControl.DataReceivedEventHandler DataReceived;
        void sc_DataReceived(object sender, SerialReceivedEventArgs e)
        {
            if (DataReceived != null) DataReceived(this, e);
        }
        public event serialControl.PortOpeningHandler PortOpening;
        void sc_PortOpening(object sender, EventArgs e)
        {
            if (PortOpening != null) PortOpening(this, e);
        }
        public event serialControl.PortOpenedHandler PortOpened;
        void sc_PortOpened(object sender, EventArgs e)
        {
            if (PortOpened != null) PortOpened(this, e);
        }
        public event serialControl.PortErrorEventHandler PortError; 
        void sc_PortError(object sender, PortErrorEventArgs e)
        {
            if (PortError != null) PortError(this, e);
        }
        public event serialControl.PortClosedHandler PortClosed;
        void sc_PortClosed(object sender, EventArgs e)
        {
            if (PortClosed != null) PortClosed(this, e);
        }
        protected override void OnUnsubscribeControlEvents(Control c)
        {
            base.OnUnsubscribeControlEvents(c);
            serialControl sc = (serialControl)c;
            sc.PortClosed -= new serialControl.PortClosedHandler(sc_PortClosed);
            sc.PortError -= new serialControl.PortErrorEventHandler(sc_PortError);
            sc.PortOpened -= new serialControl.PortOpenedHandler(sc_PortOpened);
            sc.PortOpening -= new serialControl.PortOpeningHandler(sc_PortOpening);
        }
    }
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.MenuStrip |
                                       ToolStripItemDesignerAvailability.ContextMenuStrip |
                                       ToolStripItemDesignerAvailability.StatusStrip)]
    public class ComboStripItem : FixedToolStripControlHost
    {
        private ComboBoxCustom combo;

        public ComboStripItem()
            : base(new ComboBoxCustom())
        {
            this.combo = this.Control as ComboBoxCustom;
            
        }

        // Add properties, events etc. you want to expose...
        //[Browsable(true)]
        //public new Image Image { get { return pic.Image; } set { pic.Image = value; } }
        //public PictureBoxSizeMode SizeMode { get { return pic.SizeMode; } set { pic.SizeMode = value; } }
        public Color BorderColor
        {
            get { return combo.BorderColor; }
            set { combo.BorderColor = value; }
        }
        public ButtonBorderStyle BorderStyle
        {
            get { return combo.BorderStyle; }
            set { combo.BorderStyle = value; }
        }
        public int SelectedIndex
        {
            get { return combo.SelectedIndex; }
            set { combo.SelectedIndex = value; }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
         Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        public ComboBox.ObjectCollection Items
        {
            get { return combo.Items; }
        }
        protected override void OnSubscribeControlEvents(Control c)
        {
            base.OnSubscribeControlEvents(c);
            ComboBoxCustom cbc = (ComboBoxCustom)c;
            cbc.SelectedIndexChanged += new EventHandler(cbc_SelectedIndexChanged);
        }
        public event EventHandler SelectedIndexChanged;
        void cbc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SelectedIndexChanged != null) SelectedIndexChanged(this, e);
        }
        protected override void OnUnsubscribeControlEvents(Control c)
        {
            base.OnUnsubscribeControlEvents(c);
            ComboBoxCustom cbc = (ComboBoxCustom)c;
            cbc.SelectedIndexChanged -= new EventHandler(cbc_SelectedIndexChanged);
        }
    }

    //http://stackoverflow.com/questions/8856394/net-custom-control-toolstripcontrolhost-wreaks-havoc-on-the-designer
    public class FixedToolStripControlHost : ToolStripControlHost
    {
        public FixedToolStripControlHost()
            : base(new Control()) { }
        public FixedToolStripControlHost(Control c)
            : base(c) { }
    }
}
